package com.dongdongshop.controller;

import com.dongdongshop.constant.ResultConstant;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.exception.LoginException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    @RequestMapping("toLogin")
    public String toLogin(){
        return "shoplogin";
    }

    @RequestMapping("login")
    @ResponseBody
    public Result login(String username, String password){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try {
            subject.login(token);
        }catch (UnknownAccountException e1){
            System.out.println("账号不存在");
            return Result.build(ConstantEnum.ERROR);
        }catch (IncorrectCredentialsException e2){
            System.out.println("密码错误");
            return Result.build(ConstantEnum.ERROR);
        }catch (LoginException e3){
            System.out.println("账户异常");
            return Result.build(ConstantEnum.ERROR);
        }
        System.out.println("登录成功");
        return Result.build(ConstantEnum.SUCCESS);
    }
}
